//
//  KlineStyle.swift
//  Exchange
//
//  Created by rui on 2018/5/2.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

extension CHKLineChartStyle {
    
    public static var customDark: CHKLineChartStyle {
        
        let style = CHKLineChartStyle()
        style.labelFont = UIFont.systemFont(ofSize: 9)
        style.lineColor = UIColor(hexStr: "#303743")
        style.textColor = UIColor(hexStr: "#616979")
        style.selectedBGColor = UIColor(white: 0.4, alpha: 1)
        style.selectedTextColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
        style.padding = UIEdgeInsets(top: 32, left: 8, bottom: 4, right: 8)
        style.backgroundColor = UIColor(hexStr: "#262c37")
        style.showYAxisLabel = .right
        style.isInnerYAxis = true
        
        //配置图表处理算法
        style.algorithms = [
            CHChartAlgorithm.timeline,
            CHChartAlgorithm.sar(4, 0.02, 0.2), //默认周期4，最小加速0.02，最大加速0.2
            CHChartAlgorithm.ma(5),
            CHChartAlgorithm.ma(10),
            CHChartAlgorithm.ma(30)
        ]
        
        //分区点线样式
        let upcolor = (UIColor.ch_hex(0x3cb265), true)
        let downcolor = (UIColor.ch_hex(0xeb4949), true)
        let priceSection = CHSection()
        priceSection.backgroundColor = style.backgroundColor
        priceSection.titleShowOutSide = true
        priceSection.valueType = .master
        priceSection.key = "master"
        priceSection.hidden = false
        priceSection.ratios = 3
        priceSection.padding = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
        priceSection.yAxis.referenceStyle = .solid(color: UIColor(hexStr: "#303743"))
        priceSection.xAxis.referenceStyle = .solid(color: UIColor(hexStr: "#303743"))
        priceSection.xAxis.tickInterval = 4
        priceSection.yAxis.tickInterval = 4
        
        /// 时分线
        let timelineSeries = CHSeries.getTimelinePrice(
            color: UIColor(hexStr: "#325de9"),
            section: priceSection,
            showGuide: true,
            lineWidth: 1)
        
        timelineSeries.hidden = true
        timelineSeries.key = CHSeriesKey.timeline//"timeline"
        timelineSeries.showTitle = false
        
        let timeMASeries = CHSeries.getPriceMA(
            isEMA: false,
            num: [5],
            colors: [
                UIColor.ch_hex(0x8e4cd8)
            ],
            section: priceSection)
        timeMASeries.hidden = true
        
        timeMASeries.key = "timelineMA"
        
        
        /// 蜡烛线
        let priceSeries = CHSeries.getCandlePrice(
            upStyle: upcolor,
            downStyle: downcolor,
            titleColor: UIColor(white: 0.8, alpha: 1),
            section: priceSection,
            showGuide: true,
            ultimateValueStyle: .arrow(UIColor(white: 0.8, alpha: 1)))
        
        priceSeries.showTitle = false
        
        priceSeries.chartModels.first?.ultimateValueStyle = .arrow(UIColor(white: 0.8, alpha: 1))
        
        priceSeries.key = CHSeriesKey.candle//"price"
        
        let priceMASeries = CHSeries.getPriceMA(
            isEMA: false,
            num: [5,10,30],
            colors: [
                UIColor.ch_hex(0x8e4cd8),
                UIColor.ch_hex(0x697284),
                UIColor.ch_hex(0x91843b),
                ],
            section: priceSection)
        priceMASeries.hidden = false
        
        priceMASeries.key = "priceMA"
        
        
        priceSection.series = [
            timelineSeries,
            timeMASeries,
            priceSeries,
            priceMASeries,
        ]
        
        let volumeSection = CHSection()
        volumeSection.backgroundColor = style.backgroundColor
        volumeSection.valueType = .assistant
        volumeSection.key = "volume"
        volumeSection.hidden = false
        volumeSection.ratios = 1
        volumeSection.yAxis.referenceStyle = .solid(color: UIColor(hexStr: "#303743"))
        volumeSection.xAxis.referenceStyle = .solid(color: UIColor(hexStr: "#303743"))
        volumeSection.xAxis.tickInterval = 4
        volumeSection.yAxis.tickInterval = 1
        volumeSection.padding = UIEdgeInsets(top: 16, left: 0, bottom: 8, right: 0)
        
        let volumeSeries = CHSeries.getDefaultVolume(upStyle: upcolor, downStyle: downcolor, section: volumeSection)
        
        volumeSeries.key = "volume"
        
        let volumeMASeries = CHSeries.getVolumeMA(
            isEMA: false,
            num: [5,10,30],
            colors: [
                UIColor.ch_hex(0x8e4cd8),
                UIColor.ch_hex(0x697284),
                UIColor.ch_hex(0x91843b),
                ],
            section: volumeSection)
        
        volumeMASeries.key = "volumeMA"
        
        volumeSection.series = [volumeSeries, volumeMASeries]
        
        style.sections = [priceSection, volumeSection]
        
        return style
    }
    
    
    public static var customLight: CHKLineChartStyle {
        
        let style = CHKLineChartStyle()
        style.labelFont = UIFont.systemFont(ofSize: 9)
        style.lineColor = UIColor(hexStr: "#e8eaed")
        style.textColor = UIColor(hexStr: "#b1b2b2")
        style.selectedBGColor = UIColor(white: 0.4, alpha: 1)
        style.selectedTextColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
        style.padding = UIEdgeInsets(top: 32, left: 8, bottom: 4, right: 8)
        style.backgroundColor = UIColor(hexStr: "#f7f8f9")
        style.showYAxisLabel = .right
        style.isInnerYAxis = true
        
        //配置图表处理算法
        style.algorithms = [
            CHChartAlgorithm.timeline,
            CHChartAlgorithm.sar(4, 0.02, 0.2), //默认周期4，最小加速0.02，最大加速0.2
            CHChartAlgorithm.ma(5),
            CHChartAlgorithm.ma(10),
            CHChartAlgorithm.ma(30)
        ]
        
        //分区点线样式
        let upcolor = (UIColor.ch_hex(0x3cb265), true)
        let downcolor = (UIColor.ch_hex(0xeb4949), true)
        let priceSection = CHSection()
        priceSection.backgroundColor = style.backgroundColor
        priceSection.titleShowOutSide = true
        priceSection.valueType = .master
        priceSection.key = "master"
        priceSection.hidden = false
        priceSection.ratios = 3
        priceSection.padding = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
        priceSection.yAxis.referenceStyle = .solid(color: UIColor(hexStr: "#e8eaed"))
        priceSection.xAxis.referenceStyle = .solid(color: UIColor(hexStr: "#e8eaed"))
        priceSection.xAxis.tickInterval = 4
        priceSection.yAxis.tickInterval = 4
        
        /// 时分线
        let timelineSeries = CHSeries.getTimelinePrice(
            color: UIColor(hexStr: "#325de9"),
            section: priceSection,
            showGuide: true,
            lineWidth: 1)
        
        timelineSeries.hidden = true
        timelineSeries.key = CHSeriesKey.timeline//"timeline"
        timelineSeries.showTitle = false
        
        let timeMASeries = CHSeries.getPriceMA(
            isEMA: false,
            num: [5],
            colors: [
                UIColor.ch_hex(0x8e4cd8)
            ],
            section: priceSection)
        timeMASeries.hidden = true
        
        timeMASeries.key = "timelineMA"
        
        
        /// 蜡烛线
        let priceSeries = CHSeries.getCandlePrice(
            upStyle: upcolor,
            downStyle: downcolor,
            titleColor: UIColor(white: 0.8, alpha: 1),
            section: priceSection,
            showGuide: true,
            ultimateValueStyle: .arrow(UIColor(white: 0.8, alpha: 1)))
        
        priceSeries.showTitle = false
        
        priceSeries.chartModels.first?.ultimateValueStyle = .arrow(UIColor(white: 0.8, alpha: 1))
        
        
        priceSeries.key = CHSeriesKey.candle//"price"
        
        let priceMASeries = CHSeries.getPriceMA(
            isEMA: false,
            num: [5,10,30],
            colors: [
                UIColor.ch_hex(0x8e4cd8),
                UIColor.ch_hex(0x697284),
                UIColor.ch_hex(0x91843b),
                ],
            section: priceSection)
        priceMASeries.hidden = false
        
        priceMASeries.key = "priceMA"
        
        
        priceSection.series = [
            timelineSeries,
            timeMASeries,
            priceSeries,
            priceMASeries,
        ]
        
        let volumeSection = CHSection()
        volumeSection.backgroundColor = style.backgroundColor
        volumeSection.valueType = .assistant
        volumeSection.key = "volume"
        volumeSection.hidden = false
        volumeSection.ratios = 1
        volumeSection.yAxis.referenceStyle = .solid(color: UIColor(hexStr: "#e8eaed"))
        volumeSection.xAxis.referenceStyle = .solid(color: UIColor(hexStr: "#e8eaed"))
        volumeSection.xAxis.tickInterval = 4
        volumeSection.yAxis.tickInterval = 1
        volumeSection.padding = UIEdgeInsets(top: 16, left: 0, bottom: 8, right: 0)
        
        let volumeSeries = CHSeries.getDefaultVolume(upStyle: upcolor, downStyle: downcolor, section: volumeSection)
        
        volumeSeries.key = "volume"
        
        let volumeMASeries = CHSeries.getVolumeMA(
            isEMA: false,
            num: [5,10,30],
            colors: [
                UIColor.ch_hex(0x8e4cd8),
                UIColor.ch_hex(0x697284),
                UIColor.ch_hex(0x91843b),
                ],
            section: volumeSection)
        
        volumeMASeries.key = "volumeMA"
        
        volumeSection.series = [volumeSeries, volumeMASeries]
        
        style.sections = [priceSection, volumeSection]
        
        return style
    }
    
}
